#ifndef _FONT_MANAGER_H_
#define _FONT_MANAGER_H_

#include "math/vec2.h"
#include "font.h"
#include "num/named_set.h"
#include "graphics/color.h"
#include <vector>

/** 
 * FontManager:
 * \brief Creates and manages Fonts and Glyphs
 */
class FontManager : public num::NamedSet<Font*>
{
private:
	/** 
	* Cache debug print-statements fired of during the program run
	*/
	struct PrintCached
	{
		std::string     Text;
		Font*			pFont;
		math::Vec2      Pos;
		COLOR			Color;
	} ;

public:
    /**
     * Singleton accessor
     */
    static FontManager&   Instance();

    /**
     * Initialize the data + default font. Font's stored with internal name "default"
     * @param _DefaultFont      name of TTF file to create default font from
     * @param _DefaultSize      height (in points / pixels) of default font
     * @param _DefaultFlags     flags to create default font with. Defined by or'ing FontFlags
     */
    bool			Init(   const std::string&  _DefaultFont = "Impact",
							int                 _DefaultHeight = 32,
							int                 _DefaultWidth  = 32,
                            int                 _DefaultFlags = 0);

    /**
     * Destroy all data
     */
    bool			Shutdown();

    /**
     * Create a font from True Type Font (TTF) file
     * @param _FontName     internal name, i.e. Ariel12
     * @param _FileName     name of TTF file to create font from
     * @param _Height       height (in points / pixels) of font
     * @param _Flags        flags to create font with. Defined by or'ing FontFlags
     *                       
     */
     Font*  InitFont(      const std::string& _FontName,
                             const std::string& _Face,
                             unsigned long      _Width,
                             unsigned long      _Height,
							 bool				_Italic = false,
							 int				Weight = 700,
							 unsigned long		CharSet = 0);

      Font*  InitBitmapFont( const std::string& _FontName,
                             const std::string& _File);

	 /**
     * Retrieve a font cached internally.
     * Will return the default font if not found.
     * @param _FontName     internal name, i.e. Ariel12
     */
     Font*   GetFont( const std::string& _FontName)
	 {
		 Font* pFont = GetElement(_FontName);
		 if (!pFont) return NULL;
		 return pFont;
	 }

    /**
     * Retrieves the default font
     */
      Font*   GetDefaultFont() const
	  {
			return m_pDefaultFont;
	  }
    
    /**
     * Changes the default font
     */
	  void		SetDefaultFont(Font* pFont)
	  {
		m_pDefaultFont = pFont;
	  }
    /**
     * fast function to print with the default font to a specified point on screen.
     * it's a static function to simplify the syntax (it invokes the singleton internally)
     * positional parameters are in range 0..1
     * @param _Pos   Pos of text top left
     * @param _Msg printf style input. If anyone wants to build a iostream equivalent, feel free
     */
	void   Print(const math::Vec2& _Pos, const char*  _Msg, ...);
	void   Print(Font* pUseFont, const math::Vec2& _Pos, const char*  _Msg, ...);
	void   PrintCentered(const math::Vec2& _Pos, const char*  _Msg, ...);
	void   PrintCentered(Font* pUseFont, const math::Vec2& _Pos, const char*  _Msg, ...);

    /**
     * Print any cached string added with Print
     * Remember to set to 2D mode before calling this
     * @param _pBuf     Buffer to render into
     */
    void			RenderCache();
	void			SetColor(COLOR	c) { m_DefaultColor = c; };


	void			Flush();
	void			Recreate();
	void			GetFonts(std::vector<Font*>& Set);
private:
    /**
     * Constructor
     */
                    FontManager(  );
    /**
     * delete all buffers
     *  
     */
                    ~FontManager();

                /// Not implemented to prevent copying
    FontManager( const FontManager & );     
    FontManager& operator=( const FontManager & );

 
    Font*						m_pDefaultFont;
	COLOR						m_DefaultColor;
    std::vector<PrintCached>    m_PrintCache;
};

#endif // _FONT_MANAGER_H_